package com.etc.security.controller;



import javax.servlet.http.HttpServletRequest;


import com.etc.security.config.JwtTokenUtils;
import com.etc.security.dao.AdminRoleRefRepository;
import com.etc.security.dao.UserRepository;

import com.etc.security.po.AdminMst;
import com.etc.security.po.AdminRoleRef;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.etc.common.Code;
import com.etc.common.CodeMessage;


/**
 * 
 * Filename:    AuthController.java  
 * Description:   Mock 注册操作 用于获取密码
 * Copyright:   Copyright (c) 2018-2019 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 * @author:     mazheng 
 * @version:    1.0  
 * Create at:   2020年3月4日 下午7:16:55  
 *  
 * Modification History:  
 * Date         Author      Version     Description  
 * ------------------------------------------------------------------  
 * 2020年3月4日      mazheng      1.0         1.0 Version  
 *
 */
@Api(tags = "新しユーザ記入", value = "/auth/regist|/auth/checkRole")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private AdminRoleRefRepository adminRoleRefRepository;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @ApiOperation(value="/auth/regist",tags={"新しユーザ記入"})
    @ApiImplicitParams(
    @ApiImplicitParam(name="user",value="ユーザモード", required=true,paramType="form", dataType = "AdminMst",
    example = "{\"username\":\"13591976256\",\"password\": \"123456\", \"fullname\": \"mazheng\", \"role\": \"ROLE_ADMIN\"}")
    )
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "sucess"),
            @ApiResponse(code = 201, message = "regist 失敗")
    })
    @PostMapping("/register")
    @Transactional(rollbackFor = Exception.class)
    public String registerUser(@RequestBody AdminMst user){
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));

        AdminMst save = userRepository.save(user);
        AdminRoleRef condition = new AdminRoleRef();

        condition.setRid(2);
        condition.setAid(save.getId());
        AdminRoleRef ref =adminRoleRefRepository.save(condition);
        return save.toString();
    }
    @ApiOperation(value="/auth/checkRole",tags={"ロールチャック"})
    @ApiImplicitParam(paramType = "header", name = "Authorization", value = "ユーザ識別子", required = true, dataType = "string", example = "Bearer-eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTk5ODY5NzM5MCIsImlzcyI6InJjY3AiLCJleHAiOjE2MTI0ODg5ODgsImlhdCI6MTYxMTg4NDE4OCwicm9sIjoiUk9MRV9BRE1JTiJ9.GllT_I5MHV0R7GQ0LZmtYM-ihpemn9OYCEkBeFa6JU3HXxA40PB6d2H1bKT7PZMidL3pi11wPdMaMGK0pqKe0Q")
    @PostMapping("/checkRole")
    public CodeMessage checkRole(AdminMst user, @RequestHeader String Authorization, HttpServletRequest req){
        user.setUsername(JwtTokenUtils.getUsername(Authorization));
        user.setRoles(JwtTokenUtils.getUserRole(Authorization));
        return CodeMessage.success(Code.SUCESS,  user);
    }
}
